/**
 * Created by JustinChow on 2016/11/3.
 */
"use strict";

const Ws = require("ws");
const Logger = require("pomelo-logger").getLogger("TestCase", __filename);

const Configs = require("../Configs/MultiServer.json");
const APIStaticConfig = require("../Configs/APIStaticConfig.json");

const DataProvider = require("../Database/DataProvider");

require("../Utils/Methematics");

function randTankTarget (){
    var rand = Math.random();
    var nagtive = Math.floor(rand * 10) % 2 ? -1 : 1;
    var v = Math.RadianToXY(nagtive * rand * Math.PI);
    return {
        "rotation": Math.floor(nagtive * rand * 180), // ±180°
        "radian": nagtive * rand * Math.PI, // ±π
        "x": v.x,  // 0-maxW
        "y": v.y   // 0-maxH
    };
}

function randProps() {
    var idx = Math.floor(Math.random() * PropsIds.length);
    return {"propsId": PropsIds[idx]};
}





var PlayerId = 0;
var ObjectId = 0;
var PropsIds = [];

//Logger.debug("ws://" + Configs.host + ":" + Configs.ports[0]);
var ws = new Ws("ws://" + Configs.host + ":" + Configs.ports[0]);

ws.reconnect = function () {
    // setTimeout(function (){
    //     ws = new Ws("ws://", Configs.host + ":" + Configs.ports[0]);
    // }, 2000);
};

ws.sendPacket = function (packetId, msg) {
    if (!msg) msg = {};

    msg.packetId = packetId;
    ws.send(JSON.stringify(msg), function (err) {
        if (err) {
            Logger.debug("Send [ %s ] Data [ %j ] Ret: ", packetId, msg, err);
            // ws.reconnect();
        }
    });
};

ws.on("open", function open() {
    Object.keys(DataProvider.getProps()).forEach(propsId => {
        PropsIds.push(propsId);
    });
    ws.sendPacket(APIStaticConfig.ON_SET_PROTOCOL_VERSION, {"protocolVersion": 5});
    setInterval(function () {
        ws.sendPacket(APIStaticConfig.ON_SET_TARGET, randTankTarget());
        ws.sendPacket(APIStaticConfig.ON_USE_PROPS, randProps());
    }, 2000);
});

ws.on("message", function (data, flags) {
    // Logger.debug("Receive Server Data [ %s ] Flags [ %j ]", data, flags);
    data = JSON.parse(data);
    switch (data.packetId) {
        // Update Whole Player List (Visible, nonVisible, Dead Ones)
        case APIStaticConfig.TO_UPDATE_MAP:
            // Logger.debug("Receive Server Update Map Data [ %s ] [ %j ] Flags [ %j ]", data.packetId, data, flags);
            // if (data.dead.length > 0) {
            //     Logger.debug("Receive Server Update Map Data Other Player Dies [ %j ]", data.dead);
            // }
            //
            // if (data.enemy.length > 0) {
            //     Logger.debug("Receive Server Update Map Data Enemy List [ %j ]", data.enemy);
            // } else {
            //     Logger.warn("Receive Server Update Map Data No Enemy");
            // }
            //
            // if (data.nonVisible.length >0) {
            //     Logger.debug("Receive Server Update Map Data Remove nonVisible User [ %j ] [ %j ]", data.nonVisible, data.enemy);
            // }
            break;
        // Add Player
        case APIStaticConfig.TO_ADD_PLAYER:
            Logger.debug("Receive Server Add Player Data [ %s ] [ %j ]", data.packetId, data);
            ObjectId = data.objectId;
            ws.sendPacket(APIStaticConfig.ON_SET_BOT);
            break;
        default:
            // Logger.debug("Receive Server Data [ %s ] [ %j ] Flags [ %j ]", data.packetId, data, flags);
            Logger.debug("Receive Server Data [ %s ] [ %j ]", data.packetId, data);
            break;
    }
});

ws.on("error", function (err) {
    Logger.error("WebSocket Connection Happens: ", err);
    // ws.reconnect();
});
